package de.ingo.app.mensa.v0_2.helperclasses;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import android.util.Log;

public class DatumHelper {
	
    private static final String infoTAG 		= "TESTING_APP_DATUMHELPER";

	
	/** 
	 * Compares a given Date with the actual Date
	 * 
	 * @param datumString in format dd.mm.yyyy
	 * 
	 * @return boolean if true than Date is today else false
	 * 
	 * @author ingorichter
	 */
	public static boolean isActualDate(String datumString) {		
		Date heute 				= new Date(System.currentTimeMillis());
		Calendar heuteCal 		= Calendar.getInstance();
		heuteCal.setTime(heute);
	
		String heuteString 		= (heuteCal.get(Calendar.DAY_OF_MONTH) + 
									"." + 
									(heuteCal.get(Calendar.MONTH)+1) + 
									"." + 
									heuteCal.get(Calendar.YEAR));
		
   		Log.v(infoTAG, "isActualDate | heutiges Datum: " + heuteString + " | Datum zum vergleichen: " + datumString);
		
		if (datumString.compareTo(heuteString) == 0) 
			return true; 
		else
			return false;
	}
	
	
	/**
	 * Compares if a given Date is a Date in the Future		
	 *
	 * @param datumString in format dd.mm.yyyy
	 * 
	 * @return boolean if true than Date is a Date in the Future else false
	 * 
	 * @author ingorichter
	 */
	public static boolean isFutureDate(String datumString) {		
		Date heute 				= new Date(System.currentTimeMillis());
		Calendar heuteCal 		= Calendar.getInstance();
		heuteCal.setTime(heute);
		Date datumDate			= null;
		Calendar datumCal		= Calendar.getInstance();
		SimpleDateFormat sdf	= new SimpleDateFormat("dd.MM.yyyy");
	
		try {
			datumDate = sdf.parse(datumString);
		}
		catch (ParseException e) {
			e.printStackTrace();
		}

		datumCal.setTime(datumDate);						

		Log.v(infoTAG, "isFutureDate | Datum zum testen: " + datumString + " | liegt in Zukunft? : " + (datumCal.after(heuteCal)));

		
		return (datumCal.after(heuteCal));
	}
	
	
	/**
	 * Finds out which Day belongs to the Date (is 12.06.11 a Montag or Dienstag or...)
	 * 
	 * @param dateString in format dd.mm.yyyy
	 * 
	 * @return String Name of the Day (Montag, Dienstag,...)
	 * 
	 * @author ingorichter
	 */
	public static String getDayOfDate (String dateString) {
		
		SimpleDateFormat sdf	= new SimpleDateFormat("dd.MM.yyyy");
		Date date 				= null;
		
		try {
			date = sdf.parse(dateString);
		}
		catch (ParseException e) {
			e.printStackTrace();
		}
		
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
				
		switch (cal.get(Calendar.DAY_OF_WEEK)) {
			case 1 : 
				dateString = "Sonntag";
				break;
			case 2 : 
				dateString = "Montag";
				break;
			case 3 : 
				dateString = "Dienstag";
				break;
			case 4 : 
				dateString = "Mittwoch";
				break;
			case 5 : 
				dateString = "Donnerstag";
				break;
			case 6 : 
				dateString = "Freitag";
				break;
			case 7 : 
				dateString = "Samstag";
				break;
		}
		return dateString;
	}
	
	
	/**
	 * Gets the Date of Today.
	 * 
	 * @return String in format dd.mm.yyyy
	 * 
	 * @author ingorichter
	 */
	public static String getToday() {
		Date today = new Date(System.currentTimeMillis());
		Calendar todayCal = Calendar.getInstance();
		todayCal.setTime(today);	
		
		String todayString = (	todayCal.get(Calendar.DAY_OF_MONTH) + 
								"." + 
								(todayCal.get(Calendar.MONTH)+1) + 
								"." + 
								todayCal.get(Calendar.YEAR));
		
		return todayString;
	}
	
	
	public static boolean isBeforeFifteen() {
		Date zeitpunkt = new Date(System.currentTimeMillis());
		Calendar zeitpunktCal = Calendar.getInstance();
		zeitpunktCal.setTime(zeitpunkt);
		
		Log.v(infoTAG, "isVor15: " + zeitpunktCal.get(Calendar.HOUR_OF_DAY));
		
		if (zeitpunktCal.get(Calendar.HOUR_OF_DAY) < 15) {
			return true;
		} else
			return false;
	}
	
	public static boolean isBeforeEight() {
		Date zeitpunkt = new Date(System.currentTimeMillis());
		Calendar zeitpunktCal = Calendar.getInstance();
		zeitpunktCal.setTime(zeitpunkt);
		
		Log.v(infoTAG, "isVor8: " + zeitpunktCal.get(Calendar.HOUR_OF_DAY));

		if (zeitpunktCal.get(Calendar.HOUR_OF_DAY) < 8) {
			return true;
		} else
			return false;
	}
	
	public static boolean isBeforeTwelve() {
		Date zeitpunkt = new Date(System.currentTimeMillis());
		Calendar zeitpunktCal = Calendar.getInstance();
		zeitpunktCal.setTime(zeitpunkt);
		
		Log.v(infoTAG, "isVor12: " + zeitpunktCal.get(Calendar.HOUR_OF_DAY));

		if (zeitpunktCal.get(Calendar.HOUR_OF_DAY) < 12) {
			return true;
		} else
			return false;
	}
}
